<template>
    <div class="app-container">
      <el-card style="border-radius: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="单据编码" prop="sourceDocCode">
          <el-input
            v-model="queryParams.sourceDocCode"
            placeholder="请输入单据编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物资名称" prop="itemName">
          <el-input
            v-model="queryParams.itemName"
            placeholder="请输入物资名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="检测类型" prop="qcType">
          <el-select v-model="queryParams.qcType" placeholder="请选择检测类型" clearable>
            <el-option
              v-for="dict in qcTypeOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工序名称" prop="processName">
          <el-input
            v-model="queryParams.processName"
            placeholder="请输入工序名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      </el-card>
      <div style="height: 15px"></div>
      <el-card style="border-radius: 10px">
      <el-table border v-loading="loading" :data="pendingList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="单据编码" align="center" prop="sourceDocCode" />
        <el-table-column label="检测类型" align="center" prop="qcType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.mes_qc_type" :value="scope.row.qcType"/>
          </template>
        </el-table-column>
        <el-table-column label="物料编码" align="center" prop="itemCode" />
        <el-table-column label="物料名称" align="center" prop="itemName" />
        <el-table-column label="待检数量" align="center" prop="quantityUncheck" >
          <template slot-scope="scope">
            <el-tag type="danger">{{ scope.row.quantityUncheck }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="工序名称" width="150px" align="center" prop="processName"/>
        <el-table-column label="位置" align="center" prop="address" />

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              v-if="scope.row.qcType == 'RQC'"
              @click="handleAddRQC(scope.row)"
              v-hasPermi="['mes:qc:ipqc:edit']"
            >返修检验</el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleRQCExempt(scope.row)"
              v-if="scope.row.qcType == 'RQC'"
              v-hasPermi="['mes:qc:ipqc:edit']"
            >免检</el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleAddIQC(scope.row)"
              v-if="scope.row.qcType == 'IQC'"
              v-hasPermi="['mes:qc:iqc:edit']"
            >来料检验</el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleIqcExempt(scope.row)"
              v-if="scope.row.qcType == 'IQC'"
              v-hasPermi="['mes:qc:iqc:exempt']"
            >免检</el-button>

            <el-button
              size="mini"
              type="text"
              @click="handleAddOSQC(scope.row)"
              v-if="scope.row.qcType == 'OSQC'||scope.row.qcType == 'OSFBQC'"
              v-hasPermi="['mes:qc:osqc:add']"
            >外协检验</el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleOSQCExempt(scope.row)"
                v-if="scope.row.qcType == 'OSQC'||scope.row.qcType == 'OSFBQC'"
                v-hasPermi="['mes:qc:osqc:exempt']"
              >免检</el-button>
            <el-button
              size="mini"
              type="text"
              v-if="scope.row.qcType == 'PQC'"
              @click="handleAddPQC(scope.row)"
              v-hasPermi="['mes:qc:ipqc:edit']"
            >过程检验</el-button>
            <el-button
              size="mini"
              type="text"
              v-if="scope.row.qcType === 'OQC'"
              @click="handleAddOQC(scope.row)"
              v-hasPermi="['mes:qc:oqc:edit']"
            >出货检验</el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleOqcExempt(scope.row)"
              v-if="scope.row.qcType === 'OQC'"
              v-hasPermi="['mes:qc:oqc:exempt']"
            >免检</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      </el-card>
    </div>
  </template>

  <script>
  import { listPending } from "@/api/mes/qc/pending";
  import { exemptOsqc } from "@/api/mes/qc/osqc"
  import { exemptOqc } from "@/api/mes/qc/oqc"
  import { exemptIqc } from "@/api/mes/qc/iqc"
  import {exemptRqc} from "@/api/mes/qc/rrqc";

  export default {
    name: "Qcpending",
    dicts: ['mes_qc_type'],
    data() {
      return {
        //自动生成编码
        autoGenFlag:false,
        optType: undefined,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 检测项表格数据
        pendingList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          sourceDocCode: null,
          itemName: null,
          qcType: null,
          processName: null,
        },
        // 表单参数
        form: {},
        // 检查类型
        qcTypeOptions: [
          {label: '来料检验', value: 'IQC'},
          {label: '过程检验', value: 'PQC'},
          {label: '出货检验', value: 'OQC'},
          {label: '外协检验', value: 'OSQC'},
          {label: '返修检验', value: 'RQC'}],
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询待检测任务列表 */
      getList() {
        this.loading = true;
        listPending(this.queryParams).then(response => {
          this.pendingList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.indexId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 返修单检验单 */
      handleAddRQC(row) {
        this.$router.push({name: 'rqcadd', params: row});
      },
      /*返修免检*/
      handleRQCExempt(row){
        console.log(row);
        let that=this;
        this.$modal.confirm('是否确认免检？').then(function () {
          exemptRqc(row).then(function (data) {
            if(data.code==200){
              that.$modal.msgSuccess("操作成功");
              that.getList();
            }else{
              that.$modal.msgError(data.msg);
            }
          });
        });
      },
      // 生成来料检验单
      handleAddIQC(row){
        this.$router.push({name: 'iqcadd', params: row});
      },
      // 生成外协检验单
      handleAddOSQC(row){
        this.$router.push({name: 'osqcadd', params: row});
      },
      handleOSQCExempt(row){
        console.log(row);
        let that=this;
        this.$modal.confirm('是否确认免检？').then(function () {
          exemptOsqc(row).then(function (data) {
            if(data.code==200){
              that.$modal.msgSuccess("操作成功");
              that.getList();
            }else{
              that.$modal.msgError(data.msg);
            }
          });
        });
      },
      /** 生成过程检验单 */
      handleAddPQC(row) {
        this.$router.push({name: 'pqcadd', params: row});
      },
      /** 生成出货检验单 */
      handleAddOQC(row) {
        this.$router.push({name: 'oqcadd', params: row});
      },
      // 出货质检免检
      handleOqcExempt(row) {
        let that = this;
        row.clientId = row.vendorClientId;
        row.clientCode = row.vendorClientCode;
        row.clientName = row.vendorClientName;
        row.quantityOut = row.quantityUncheck;
        row.outDate = row.recordTime;
        this.$modal.confirm('是否确认免检？').then(function () {
          exemptOqc(row).then(function (data) {
            if(data.code === 200){
              that.$modal.msgSuccess("操作成功");
              that.getList();
            }else{
              that.$modal.msgError(data.msg);
            }
          })
        });
      },
      // 来料质检免检
      handleIqcExempt(row){
        let that = this;
        row.quantityRecived=row.quantityUncheck;
        row.vendorId = row.vendorClientId;
          row.vendorCode = row.vendorClientCode;
          row.vendorName = row.vendorClientName;
          row.vendorNick = row.vendorClientNick;
          row.vendorBatch = row.batchCode;
          row.reciveDate = row.recordTime;
        this.$modal.confirm('是否确认免检？').then(function () {
          exemptIqc(row).then(function (data) {
            if(data.code === 200){
              that.$modal.msgSuccess("操作成功");
              that.getList();
            }else{
              that.$modal.msgError(data.msg);
            }
          })
        });
      }
    }
  };
  </script>
